package personal.zq.practice.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;

@Slf4j
public class GaoDeMapUtils {

    static final String GAODE_SEARCH_URL = "https://restapi.amap.com/v5/place/text";

    public static String getUrlOfPlaceSearch(String key, String keywords, int pageNum, int pageSize) {

        String output = "JSON";

        String url = GAODE_SEARCH_URL
                // 开发者账号的申请 key
                + "?key=" + key
                // 需要查询的地址
                + "&keywords=" + keywords
                //根据区划过滤
                + "&filter=" + "全国"
                // 最外层返回数据个数
                + "&offset=" + pageSize
                // 需要第几页数据
                + "&page=" + pageNum
                // 返回数据格式类型
                + "&output=" + output;
        return url;
    }

    public static String searchByGaode(String key, String keywords, int pageSize, int pageNum, RestTemplate restTemplate) {
        if (CommonUtils.ObjectisEmpty(keywords)) {
            throw new BusinessException("高德地图->查询地址为空");
        }
        String validUrl = getUrlOfPlaceSearch(key, keywords, pageNum, pageSize);
        String response = restTemplate.getForObject(validUrl, String.class);
        return response;
    }

}
